package org.programmers.anonymous.fightrobots.service;

import java.util.ArrayList;
import java.util.List;

import org.programmers.anonymous.fightrobots.controller.IDeffenseController;
import org.programmers.anonymous.fightrobots.model.IDeffense;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/deffense-service")
public class DeffenseService implements BeanFactoryAware{
	
	private IDeffenseController deffenseController = null;
	

	@RequestMapping(value="/getDeffense", method = RequestMethod.GET)
	public @ResponseBody IDeffense receiveMessage(@RequestParam(value="deffenseId", required=true) String deffenseId, ModelMap model)
	{
		return deffenseController.loadDeffense(Integer.valueOf(deffenseId));
	}
	
	
	@RequestMapping(value="/getAllDefenses", method = RequestMethod.GET)
	public @ResponseBody List<String>  getAllDefenses()
	{
	List<String> defenseList=new ArrayList<String>();
    	 
     	for (IDeffense iDefense: deffenseController.getAllDeffenses()){
     		defenseList.add(iDefense.getName());	     		
     	}
     	
     	return defenseList;
		
	}
	
	
	/**
	* Handles request for adding a deffense
	*/
	@RequestMapping(value = "/createDeffense", method = RequestMethod.POST)
	public  void createDeffense(
	@RequestParam(value = "deffenseName", required = true) String deffenseName,
	@RequestParam(value = "deffensePower", required = true) Integer deffensePower,
	@RequestParam(value = "deffenseHits", required = true) Integer deffenseHits,
	ModelMap model) {
		deffenseController.addDeffense(deffenseController.createDeffense(deffenseName, deffensePower, deffenseHits));
	
	}
	
	@RequestMapping(value="/deleteDeffense" ,method = RequestMethod.POST)
	public @ResponseBody boolean deleteDeffense(@RequestParam(value="deffenseId", required=true) String deffenseId, ModelMap model){
	     	return deffenseController.delete(deffenseController.loadDeffense(Integer.valueOf(deffenseId)));	
	}
	
	@RequestMapping(value="/editDeffense" ,method = RequestMethod.POST)
	public @ResponseBody int editDeffense(
			@RequestParam(value = "deffenseId", required=true) int deffenseId,
			@RequestParam(value = "deffenseName", required = true) String deffenseName,
			@RequestParam(value = "deffensePower", required = true) Integer deffensePower,
			@RequestParam(value = "deffenseHits", required = true) Integer deffenseHits,
			 ModelMap model){
		
		IDeffense ideffense = deffenseController.loadDeffense(deffenseId);
		ideffense.setName(deffenseName);
		ideffense.setHits(deffenseHits);
		ideffense.setPower(deffensePower);
		return deffenseController.save(ideffense);
	

	}
	
	
	
	
	@Override
	public void setBeanFactory(BeanFactory context) throws BeansException
	{
		deffenseController = (IDeffenseController) context.getBean("deffenseController");
	}
}